
#ifndef __MSG_QUEUE_MANAGER_H__
#define __MSG_QUEUE_MANAGER_H__

#include <map>
#include <mutex>
#include <string>
#include <functional>
#include "json.h"
#include "Thread/Thread.h"
#include <queue>



namespace Media{

const uint16_t TCP_PORT_AMX = 2202;

const uint16_t TCP_PORT_CAMERA = 2203;

const uint16_t UDP_PORT_CONSOLE = 2208;

const uint16_t UDP_PORT_VISCA = 1259;

class MsgQueueManage{
public:
    typedef enum
    {
        MEDIA_REMOTE_V520D = 0,

        MEDIA_REMOTE_BUTT
    }MEDIA_REMOTE_DEV;

    const int COMMAND_MAX_LEN  = 1024;

    /* slave dev act*/
    const unsigned char PRESET_SWITCH[7] = {0x81, 0x01, 0x04, 0x3F, 0x02, 0xC9, 0xFF}; //C9/CB

    
    

    const std::string HEARD_REQ_GET = "GET";
    const std::string HEARD_REQ_SET = "SET";
    const std::string HEARD_REP = "REP";

    /* --------svrver rep -------*/
    //argc==3
    const std::string CMD_VIDEO_SPEAKING_SLAVE_DEVICE = "VIDEO_SPEAKING_SLAVE_DEVICE";
    const std::string CMD_EVENT_RES = "EVENT_RES";

    /* --------client req--------*/
    //argc==2
    const std::string CMD_KEEP_LIVE = "KEEP_LIVE"; //< GET KEEP_LIVE >
    const std::string CMD_SLAVE_DEV_IP = "SLAVE_DEV_IP"; //< GET SLAVE_DEV_IP >
    //argc==3
    const std::string CMD_SHOW_DEVICE_IP = "SHOW_DEVICE_IP"; // < SET SHOW_DEVICE_IP 192.168.16.30 >
    const std::string CMD_SLAVE_DEV_TRACK_MODE = "SLAVE_DEV_TRACK_MODE"; //< SET SLAVE_DEV_TRACK_MODE ON >
    const std::string CMD_SLAVE_DEV_POSITION = "SLAVE_DEV_POSITION"; //< SET SLAVE_DEV_POSITION 1 >



    typedef struct
    {
        MEDIA_REMOTE_DEV emDevType;        /* amx100 */
        int iDislpayStatus; /* dislpay Status */
        int keepLiveStatus; /* kepplive */
        char eventKeyValue[8]; /* KeyValu */
        std::string reponseInfo; /* response info*/
        std::string showDevIp; /* showDevIp */
        std::string slaveDevMode; /* slaveDevMode */
        int presetPosNum; /* presetPosNum */
        
        
        int iRevered[27];
    } MEDIA_shureConfig_t;
public:    
    // TCP消息集
    struct MessageBuffer 
	{
		size_t size; /*< data length*/
		uint8_t* data; /*< raw data*/
		std::string commAddr; /*< Communication ip*/
		uint16_t commPort; /*< Communication port*/

		MessageBuffer(size_t len)
		: size(len)
		, data(NULL)
		{
			data = new uint8_t[len];
		}

		~MessageBuffer()
		{
			delete []data;
			data = NULL;
		}

		MessageBuffer(const MessageBuffer&);
		MessageBuffer& operator=(const MessageBuffer&);
	};

    // 定义智能指针
    typedef std::shared_ptr<MessageBuffer> MessagePtr;

public:

	static MsgQueueManage *instance(uint16_t port);

	bool SaveToCommQueue(MessagePtr& msg);

	bool startReceiveTherad();
	
private:
	MsgQueueManage(uint16_t port);

	~MsgQueueManage();

    void ThreadProc(Fdt::Thread *thread);

    bool GetCommQueueMsg(MessagePtr& msg);

    bool initProtocolInfo();

    bool getShureCommandCnt(const unsigned char *data,int len,int *iCmdCnt);

    bool shureCommandPrase(      MessagePtr& msgPtr,const unsigned char *data, int len);

    bool shureCommandDo( MessagePtr& msgPtr);

    bool messageDeal(MessagePtr& msgPtr, int comand_argc, char** comand_argv);


private:
	static std::map<uint16_t, MsgQueueManage *>             sm_objectMap;

	static std::recursive_mutex 			                sm_mapMutex;

    std::mutex                                              m_queueMutex;
	
    std::queue<MessagePtr>                                  m_frameaQueue;

    MessagePtr                                              m_framePtr;

    uint16_t                                                m_dealPort = 0;

    std::vector<std::string>                                m_clientReqInfoVec;

    std::vector<std::string>                                m_svrRepInfoVec;

    bool                                                    m_threadFlg = false;
	
	Fdt::Thread 						                    m_thread{"MsgQueueManage"};//thread放在最后,保证thread最后构造,最先析构

};

}//namespace Media
#endif

